"""
Case Type   : USTORE
Case Name   : 验证参数enable_default_ustore_table的生效机制
Create At   : 2025/3/12
Owner       : wan005
Description :
    1.查询参数默认值
    2.修改enable_default_ustore_table值为on
    3.创建表,并插入数据
    4.查看表storage_type
Expect      :
    1.参数默认值off
    2.修改成功
    3.创建成功
    4.表storage_type=ustore
History     :
"""
import os
import unittest
import sys
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant


class Security(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'---{os.path.basename(__file__)} start---')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.table = 't_ustore_01'

    def test_ustore(self):
        text = '-----step1:查询参数默认值;expect:参数默认值off-----'
        self.logger.info(text)
        res1 = self.common.show_param('enable_default_ustore_table')
        self.assertEqual('off', res1, text + '执行失败')

        text = '----step2:修改enable_default_ustore_table值为on;expect:成功----'
        self.logger.info(text)
        self.sh_primy.execute_gsguc('reload',
            self.constant.GSGUC_SUCCESS_MSG, 'enable_default_ustore_table=on')
        res2 = self.common.show_param('enable_default_ustore_table')
        self.assertEqual('on', res2, text + '执行失败')

        text = '-----step3:创建表,并插入数据;expect:创建成功-----'
        self.logger.info(text)
        sql3 = f'drop table if exists {self.table};' \
               f'create table {self.table}(age int primary key) ' \
                   f'with (storage_type=ustore);' \
               f'insert into {self.table} values(5);'
        res3 = self.sh_primy.execut_db_sql(sql3)
        self.logger.info(res3)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res3, text + '执行失败')

        text = '----step4:查看表storage_type;expect:表storage_type=ustore----'
        self.logger.info(text)
        res4 = self.sh_primy.execut_db_sql(f'\\d+ {self.table}')
        self.logger.info(res4)
        self.assertIn('storage_type=ustore', res4, text + '执行失败')

    def tearDown(self):
        text = '-----step1:清理环境，恢复配置;expect:成功-----'
        self.logger.info(text)
        self.sh_primy.execute_gsguc('reload', self.constant.GSGUC_SUCCESS_MSG,
                                    'enable_default_ustore_table=off')
        res1 = self.common.show_param('enable_default_ustore_table')
        self.assertEqual('off', res1, text + '执行失败')
        sql_cmd2 = f'drop table if exists {self.table};'
        res2 = self.sh_primy.execut_db_sql(sql_cmd2)
        self.logger.info(res2)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res2,
                         text + '执行失败')
        self.logger.info(f'---{os.path.basename(__file__)} end---')
